package com.xunqi.weixinpar.controller;

import com.xunqi.weixinpar.entity.Order;
import com.xunqi.weixinpar.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@RequestMapping(value="/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, HttpServletResponse response, Order order){
        //商户订单号
        order.setOrderNO(UUID.randomUUID().toString().replaceAll("-", ""));
        order.setBody("充值号码："+order.getPhone()+",充值金额:"+order.getAmount()+"元");
        order.setStatus(0);

        int result=orderService.saveOrder(order);
        if (result>0){
            //跳转支付页面
            return "redirect:/order/topay?orderNo="+order.getOrderNO();
        }else{
            //下单失败
            request.setAttribute("error","下单失败");
            return "404";
        }

    }

    @RequestMapping(value = "/topay")
    public String topay(HttpServletRequest request, HttpServletResponse response, String orderNo){
        Order order=orderService.findByOrderNo(orderNo);
        request.setAttribute("order",order);
        return "pay";
    }


    @RequestMapping(value = "/success")
    public String success(HttpServletRequest request,String orderNO){
        request.getSession().setAttribute("orderNO",orderNO);
        return "success";
    }


}
